
// <copyright file="CompositeKeyValueComparer.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The composite key value comparer.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    using SubhadraSolutions.Sharp.Utils.Collections.Concurrent;

    /// <summary>
    /// The composite key value comparer.
    /// </summary>
    public sealed class CompositeKeyValueComparer : IComparer<CompositeKeyValue<string, FileInfo>>
    {
        #region Static Fields

        /// <summary>
        /// The instance.
        /// </summary>
        private static readonly CompositeKeyValueComparer InstanceObject = new CompositeKeyValueComparer();

        #endregion Static Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Prevents a default instance of the <see cref="CompositeKeyValueComparer"/> class from being created.
        /// </summary>
        private CompositeKeyValueComparer()
        {
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static CompositeKeyValueComparer Instance
        {
            get
            {
                return InstanceObject;
            }
        }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The compare.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Compare(CompositeKeyValue<string, FileInfo> x, CompositeKeyValue<string, FileInfo> y)
        {
            int cmp;
            if (x.Value != null && y.Value != null)
            {
                cmp = DateTime.Compare(x.Value.LastWriteTime, y.Value.LastWriteTime);
                return -cmp;
            }

            cmp = string.Compare(x.Key, y.Key, StringComparison.OrdinalIgnoreCase);
            return cmp != 0 ? cmp : (x.Value == null ? 1 : -1);
        }

        #endregion Public Methods and Operators
    }
}
